
let questions = [{url:"", answer:"hello"},
                {url:"", answer:"world"},
                {url: "", answer:"test"},
                {url: "", answer: "aa"}];
let answers = ["hello", "world", "test", "aa"];

const LineCtrl = require("./LineCtrl.js");
const QuestionCardCtrl = require("./QuestionCardCtrl");
const AnswerCardCtrl = require("./AnswerCardCtrl");

cc.Class({
    extends: cc.Component,

    properties: {
        linePF: cc.Prefab,
        qCards: [cc.Node],
        aCards: [cc.Node],
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.canvas = cc.director.getScene().getChildByName("Canvas");

        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchCancle, this);

        // this.test();
        this.init();
    },

    init () {
        for (let i = 0; i< this.qCards.length; i++) {
            this.qCards[i].getComponent(QuestionCardCtrl).answer = questions[i].answer;
        }
        for (let i = 0; i < this.aCards.length; i++) {
            this.aCards[i].getComponent(AnswerCardCtrl).content = answers[i];
        }
        this.startCard = null;
        this.lines = [];
    },

    test () {
        let startNode = this.qCards[0];
        let startPos = startNode.getPosition();
        startPos.y -= startNode.height/2;

        let endNode = this.aCards[2];
        let endPos = endNode.getPosition();
        endPos.y += endNode.height/2;

        let line = cc.instantiate(this.linePF);
        line.parent = startNode.parent;

        let lineCtrl = line.getComponent(LineCtrl);
        lineCtrl.init(startPos, endPos);
    },

    touchStart (event) {     
        let point = event.getLocation();
        let card = this._getTouchedCard(this.qCards, point);
        if (card) {
            this.startCard = card;
            // card.runAction(cc.blink(0.3,2));
        } else {
            this.startCard = null;
        }
    },

    touchMove (event) {

    },

    touchEnd (event) {
        let point = event.getLocation();
        let endCard = this._getTouchedCard(this.aCards, point);
        if (this.startCard && endCard) {
            cc.log("划线开始");
            
            // 删除卡片已经存在的线条
            if (this.startCard.line && cc.isValid(this.startCard.line, true)) {
                this._rmEleInArr(this.lines, this.startCard.line);
                this.startCard.line.destroy();
            }
            if (endCard.line && cc.isValid(endCard.line, true)) {
                this._rmEleInArr(this.lines, endCard.line);
                endCard.line.destroy();
            }

            // 模拟划线
            let startPos = this.startCard.getPosition();
            startPos.y -= this.startCard.height / 2;
            let endPos = endCard.getPosition();
            endPos.y += endCard.height / 2;
            let line = cc.instantiate(this.linePF);
            line.parent = this.canvas;
            this.startCard.line = line;
            endCard.line = line;
            // 通过遍历线条来确定结果
            line.start = this.startCard;
            line.end = endCard;
            this.lines.push(line);

            let lineCtrl = line.getComponent(LineCtrl);
            lineCtrl.init(startPos, endPos);

            if (this.lines.length == this.qCards.length) {
                this.scheduleOnce(()=>{
                    cc.log("连线结束");
                    let ret = this.checkResult();
                    cc.log("ret:", ret);
                    if (cc.sys.isBrowser) {
                        alert("" + (ret ? "成功" : "失败"));
                    }
                }, 0.5);;
            }
        }

    },

    _getTouchedCard (cards, touchPoint) {
        let point = this.canvas.convertToNodeSpaceAR(touchPoint);
        // cc.log(point);
        for (let card of cards) {
            let rect = card.getBoundingBox();
            if (rect.contains(point)) {
                return card;
            }
        }
    },

    _rmEleInArr (arr, ele) {
        let index = arr.indexOf(ele);
        if (index >= 0) {
            arr.splice(index, 1);
        }
        return arr;
    },

    touchCancle (event) {
        // this.touchEnd(event);
    },

    checkResult () {
        // let ret = true;
        // for (let card of this.qCards) {
        //     let endCard = card.endCard;
        //     if (!endCard) {
        //         ret = false;
        //         break;
        //     } else {
        //         if (card.getComponent(QuestionCardCtrl).answer != endCard.getComponent(AnswerCardCtrl).content) {
        //             ret = false;
        //             break;
        //         }
        //     }

        // }

        // return ret;

        let ret = true;
        if (this.lines.length < this.qCards.length) {
            ret = false;
        } else {
            for (let line of this.lines) {
                let question = line.start.getComponent(QuestionCardCtrl);
                let answer = line.end.getComponent(AnswerCardCtrl);
                if (question.answer != answer.content) {
                    ret = false;
                    break;
                }
            }
        }
        
        return ret;
    }

    // update (dt) {},
});
